<template>
  <div></div>
</template>
<script>
//微信授权中间页
export default {
  name: "author",
  data() {
    return {
      code: "",
      openId: "",
      state: "",
      userInfo: "",
      baseUrl: "",
      shareuid: "",
      activity_id: "",
      uid: ""
    };
  },
  created() {},
  mounted() {
    if (this.$route.query.state) {
      this.state = this.$route.query.state;
      //获取分享人的uid，用于绑定上级
      this.shareuid = this.getUrlParame("uid", this.state);
      //获取活动id
      this.activity_id = this.getUrlParame("activity_id", this.state);
      // console.log(this.state);
      // console.log("===========");
      // console.log(this.shareuid);
    }
    if (this.$route.query.code) {
      this.code = this.$route.query.code;
      console.log(this.code);
      this.getOpenId();
    }
    // //获取分享人的uid，用于绑定上级
    // if (this.$cookies.get("shareUid")) {
    //   this.shareuid = this.$cookies.get("shareUid");
    // }
    // //获取活动id
    // if (this.$cookies.get("activity_id")) {
    //   this.activity_id = this.$cookies.get("activity_id");
    // }
  },
  methods: {
    //获取openid
    async getOpenId() {
      let params = {
        params: {
          code: this.code,
          user_id: this.shareuid
        }
      };
      const res = await this.$axios.get(this.$api.wxLogin, params);
      if (res.code == 1) {
        this.userInfo = res.data;
        // this.openId = res.data.openid;
        this.uid = this.userInfo.user_id;
        this.$cookies.set("uid", this.userInfo.user_id);
        this.$cookies.set("token", this.userInfo.token);
        this.$cookies.set("first_login", this.userInfo.first_login);
        this.$cookies.set("temporary_token", this.userInfo.temporary_token);
        console.log("shareuid", this.shareuid);
        console.log("activity_id", this.activity_id);
        if (this.activity_id) {
          this.$router.push({
            path: this.state,
            query: {
              uid: this.uid,
              activity_id: this.activity_id
            }
          });
        } else {
          this.$router.push({
            path: this.state,
            query: {
              uid: this.uid
            }
          });
        }

        // window.location.href = this.baseUrl+'?uid='+this.uid+'&activity_id='+this.activity_id;
      } else {
        this.$toast("微信登录失败:" + res.msg);
      }
    },
    //获取
    getUrlParame(parameName, url) {
      let parames = url.substring(url.indexOf("?"));
      if (parames.indexOf(parameName) > -1) {
        let parameValue;
        if (parames.indexOf("&") > -1) {
          let arr = parames.split("&");
          let newArr = [];
          arr.forEach((item, index) => {
            let obj = {};
            obj.key = item.split("=")[0];
            obj.value = item.split("=")[1];
            newArr.push(obj);
          });
          newArr.forEach((item, index) => {
            if (item.key == parameName) {
              parameValue = item.value;
            }
          });
        } else {
          parameValue = parames.substring(
            parames.indexOf(parameName),
            parames.length
          );
          parameValue = parameValue.split("=")[1];
        }
        return parameValue;
      }
      return "";
    }
  }
};
</script>

